import pytest
import requests

from src.Api_util.conftest import api_base

class TestApiHandler01:
    sessionId = None
    userId = None
    id = None
    commodityId = None
    price = None


    @pytest.fixture(autouse=True)
    def setup(self):
        self.tools = api_base()
    def test_api_login_001(self):
        post_resp = self.tools.post(
            path = 'small/user/v1/login',
            json = {"phone": "17518836160", "pwd": "123456Aa"}
        )
        print("维度电商登录响应状态码:", post_resp.status_code)
        print("维度电商登录响应数据:", post_resp.text)
        # 断言状态码为200
        assert post_resp.status_code == 200
        # 验证响应结构
        # 断言响应中缺少result字段
        assert 'result' in post_resp.json()
        # 断言响应中有没有sessionId和userId字段"
        assert 'sessionId' in post_resp.json()['result']
        assert 'userId' in post_resp.json()['result']
        print("sessionId值:", post_resp.json()['result']['sessionId'])
        print("sessionId值:", post_resp.json()['result']['userId'])
        TestApiHandler01.sessionId = post_resp.json()['result']['sessionId']
        TestApiHandler01.userId = post_resp.json()['result']['userId']
    def test_api_002(self):
        post_resp = self.tools.get(
            path='small/commodity/v1/commodityList'
        )
        print("维度电商首页商品列表响应状态码:", post_resp.status_code)
        print("维度电商首页商品列表响应数据:", post_resp.text)

        print("商品commodityId值:", post_resp.json()['result']['rxxp']['commodityList'][0]['commodityId'])
        TestApiHandler01.commodityId = post_resp.json()['result']['rxxp']['commodityList'][0]['commodityId']
        #     断言状态码为200
        assert post_resp.status_code == 200
    def test_api_003(self):
        session_id_str = str(TestApiHandler01.sessionId)
        user_id_str = str(TestApiHandler01.userId)
        commodity_id = TestApiHandler01.commodityId
        post_resp = self.tools.post(
            path='small/circle/verify/v1/releaseCircle',
            headers={
                "userId":user_id_str,
                "sessionId":session_id_str
            },
            data = {
                "commodityId": 27,
                "content": '休闲马衔扣保暖绒里棉鞋懒人鞋毛毛鞋平底女雪地靴女短靴子豆豆鞋女鞋',
                "image":""
            }
        )
        print("发布圈子状态码:",post_resp.status_code)
        print("发布圈子响应:",post_resp.json())
    def test_api_004(self):
        session_id_str = str(TestApiHandler01.sessionId)
        user_id_str = str(TestApiHandler01.userId)
        post_resp = self.tools.get(
            path='small/circle/verify/v1/findMyCircleById',
            headers={
                "userId":user_id_str,
                "sessionId":session_id_str
            },
            params = {
                "page": 1,
                "count": 5
            }
        )
        print("发布圈子状态码:",post_resp.status_code)
        print("发布圈子响应:",post_resp.json())


if __name__ == '__main__':
    pytest.main()